package com.lin.missyou.api.v1;

import com.lin.missyou.bo.PageCounter;
import com.lin.missyou.core.LocalUser;
import com.lin.missyou.core.interceptors.ScopeLevel;
import com.lin.missyou.dto.OrderDTO;
import com.lin.missyou.exception.http.NotFoundException;
import com.lin.missyou.logic.OrderChecker;
import com.lin.missyou.model.Order;
import com.lin.missyou.service.OrderService;
import com.lin.missyou.util.CommonUtil;
import com.lin.missyou.vo.OrderIdVo;
import com.lin.missyou.vo.OrderPureVo;
import com.lin.missyou.vo.OrderSimplifyVO;
import com.lin.missyou.vo.PagingDozer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author gcq
 * @Create 2021-11-08
 */
@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    OrderService orderService;

    @Value("${missyou.order.pay-time-limit}")
    private Long payTimeLimit;

    /**
     * 下单接口
     * @param orderDTO
     * @return
     */
    @ScopeLevel()
    @PostMapping("")
    public OrderIdVo placeOrder(@RequestBody OrderDTO orderDTO)
    {
        Long uid = LocalUser.getUser().getId();
        OrderChecker orderChecker = orderService.isOk(uid, orderDTO);

        Long oid = this.orderService.placeOrder(uid, orderDTO, orderChecker);
        return new OrderIdVo(oid);
    }

    /**
     * 查询订单详细
     * @param oid
     * @return
     */
    @ScopeLevel
    @GetMapping("/detail/{id}")
    public OrderPureVo getOrderDetail(@PathVariable(name = "id") Long oid)
    {
        Optional<Order> orderDetail = this.orderService.getOrderDetail(oid);
        return orderDetail.map((o) -> new OrderPureVo(o, payTimeLimit))
                .orElseThrow(() -> new NotFoundException(50009));
    }

    /**
     * 查询未支付订单
     * @param start
     * @param count
     * @return
     */
    @ScopeLevel
    @GetMapping("/status/unpaid")
    public PagingDozer getUnpaid(@RequestParam(defaultValue = "0") Integer start,
                                 @RequestParam(defaultValue = "10") Integer count)
    {
        PageCounter page = CommonUtil.convertToPageParameter(start, count);
        Page<Order> orderPage = orderService.getUnpaid(page.getPage(), page.getCount());
        PagingDozer pagingDozer = new PagingDozer<>(orderPage, OrderSimplifyVO.class);
        pagingDozer.getItems().forEach((o) -> ((OrderSimplifyVO)o).setPeriod(payTimeLimit));
        return pagingDozer;
    }

    /**
     * 查询订单状态
     * @param status
     * @param start
     * @param count
     * @return
     */
    @ScopeLevel
    @GetMapping("/by/status/{status}")
    public PagingDozer getByStatus(@PathVariable int status,
                                   @RequestParam(defaultValue = "0") Integer start,
                                    @RequestParam(defaultValue = "10") Integer count)
    {
        PageCounter page = CommonUtil.convertToPageParameter(start, count);
        Page<Order> orderPage = orderService.getByStatus(status, page.getPage(), page.getCount());
        PagingDozer pagingDozer = new PagingDozer<>(orderPage, OrderSimplifyVO.class);
        pagingDozer.getItems().forEach((o) -> ((OrderSimplifyVO)o).setPeriod(payTimeLimit));
        return pagingDozer;
    }

}